package com.example.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.domain.question.HotQuestionVO;
import com.example.domain.question.Question;
import com.example.elasticsearch.QuestionRepository;
import com.example.mapper.question.QuestionMapper;
import com.example.redis.service.RedisService;
import com.example.security.exception.ServiceImplException;
import oj.common.core.contants.CacheConstants;
import oj.common.core.enums.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class QuestionCacheManager {
    @Autowired
    private QuestionRepository questionRepository;
    @Autowired
    private RedisService redisService;
    @Autowired
    private QuestionMapper questionMapper;

    public Long indexForList(Long questionId) {
        return 0L;
    }
    public void refreshCache() {
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .select(Question::getQuestionId).orderByDesc(Question::getCreateTime));
        if(CollectionUtil.isEmpty(questionList)) {
            return ;
        }
        List<Long> longList = questionList.stream().map(Question::getQuestionId).toList();
        redisService.rightPushAll(getQuestionListKey(),longList);
    }
    public long getHotQuestionListSize() {
        String hotQuestionListKey = getQuestionHotListKey();
        return redisService.getListSize(hotQuestionListKey);
    }
    public Long getListSize() {
        String examListKey = getQuestionListKey();
        return redisService.getListSize(examListKey);
    }

    public String getQuestionListKey() {
        return CacheConstants.QUESTION_LIST;
    }
    public String getQuestionHotListKey() {
        return CacheConstants.QUESTION_HOST_LIST;
    }

    public Long preQuestion(Long questionId) {
        String questionListKey = getQuestionListKey();
        Long index = redisService.indexOfForList(questionListKey,questionId);
        if(index == 0) {
            throw new ServiceImplException(ResultCode.FAILED_FIRST_QUESTION);
        }
        return redisService.indexForList(questionListKey,index-1,Long.class);
    }

    public Long nextQuestion(Long questionId) {
        String questionListKey = getQuestionListKey();
        Long index = redisService.indexOfForList(questionListKey,questionId);
        if(index==getListSize()-1) {
            throw new ServiceImplException(ResultCode.FAILED_LAST_QUESTION);
        }
        return redisService.indexForList(questionListKey,index+1,Long.class);
    }

    public void refreHotQuestion(List<HotQuestionVO> questionVOList) {
        if(CollectionUtil.isEmpty(questionVOList)) {
            //因为这里是从数据库当中传过来的数据，如果为空的话，没办法就是空的。
            return ;
        }
        redisService.rightPushAll(CacheConstants.QUESTION_HOST_LIST,questionVOList);
    }
}
